#ifndef __TCP_CLIENT_H
#define __TCP_CLIENT_H

#include<iostream>
#include<string>
#include<cstring>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#endif

class tcpClient{
      private:
        std::string svr_ip;
        int svr_port;
        int sock;
      public:
        tcpClient(std::string _ip="127.0.0.1" ,int _port = 8080)
          :svr_ip(_ip),
          svr_port(_port)
        {}
        void initClient()
        {
            sock = socket(AF_INET, SOCK_STREAM , 0);
            if(sock < 0)
            {
               std::cerr<<"socket  error"<<std::endl;
               exit(2);
            }
            //bind?客户端不需要绑定,也不需要监听,因为没人连,也不需要accept,因为没有listen socket
            struct sockaddr_in svr;
            svr.sin_family = AF_INET;
            svr.sin_port = htons(svr_port);
            svr.sin_addr.s_addr = inet_addr( svr_ip.c_str() ) ;
            if( connect(sock,(struct sockaddr*)&svr , sizeof(svr)) != 0 )
            {
                std::cerr<<"connect error" <<std::endl;
            }
            //connect success
        }
        void start()
        {
            char msg[64];
            while(true)
            {
               std::cout<<"Please Enter Message #";
               std::cout<<std::endl;
               size_t s = read(0,msg ,sizeof(msg)-1);
               if( s>0 )
               {
                  msg[s-1]=0; //去掉换行符,read不吸收
                  send(sock,msg,strlen(msg),0);//文件发送不用'\0'
                  size_t ss = recv(sock,msg,sizeof(msg)-1,0);
                  if( ss > 0 )
                  {
                        msg[ss] = 0;
                        std::cout<<"server echo $" <<msg <<std::endl;
                  }
               }
            }
        }
        ~tcpClient()
        {
            close(sock);
        }
};
